package com.lanou3g.musicenjoy.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.adapter.SearchingContentActivityAdapter;
import com.lanou3g.musicenjoy.fragment.SearchingAlbumFragment;
import com.lanou3g.musicenjoy.fragment.SearchingMVFragment;
import com.lanou3g.musicenjoy.fragment.SearchingSingerFragment;
import com.lanou3g.musicenjoy.fragment.SearchingSingleSongFragment;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 辛鑫 on 15/10/21.
 * //搜索点击进去以后的主fragment,容器
 */
public class SearchingContentFragment extends Fragment implements View.OnClickListener {
    @Bind(R.id.search_back)
    ImageView searchBack;
    @Bind(R.id.search_input)
    EditText searchInput;
    @Bind(R.id.search_img)
    ImageView searchImg;
    @Bind(R.id.search_content_tab)
    TabLayout searchContentTab;
    @Bind(R.id.search_content_viewPager)
    ViewPager searchContentViewPager;
    private String url;
    private SearchingContentActivityAdapter adapter;
    List<Fragment> data;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.searching_content, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        searchBack.setOnClickListener(this);
        searchContentTab.setSelectedTabIndicatorColor(getResources().getColor(R.color.colorToolbar));
        searchContentTab.setTabTextColors(getResources().getColor(R.color.colorGray), getResources().getColor(R.color.colorToolbar));
        adapter = new SearchingContentActivityAdapter(getFragmentManager());
        searchContentViewPager.setAdapter(adapter);
        searchContentTab.setupWithViewPager(searchContentViewPager);
        Bundle bundle = getArguments();
        url = bundle.getString("url");
        addFragement();
    }

    //初始化方法
    private void addFragement() {
        Log.i("Sysout","->SearchingContentFragment");
        data = new ArrayList<Fragment>();
        data.add(SearchingSingleSongFragment.passData(getActivity(), url));
        data.add(SearchingSingerFragment.passData(getActivity(), url));
        data.add(SearchingAlbumFragment.passData(getActivity(), url));
        data.add(SearchingMVFragment.passData(getActivity(), url));
        adapter.setData(data);
    }

    @Override
    public void onClick(View v) {
        getFragmentManager().popBackStack();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //将栈内存中的fragment移除掉
        if (!getActivity().isDestroyed()) {
            for(Fragment fragment:data){
                getFragmentManager().beginTransaction().remove(fragment)
                        .commit();
            }
        }
        ButterKnife.unbind(this);
    }
}
